package com.qianchang.microfinance.view.product;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.google.gson.reflect.TypeToken;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.FindUserBasicInfoBean;
import com.qianchang.microfinance.bean.LoanType;
import com.qianchang.microfinance.bean.PageBean;
import com.qianchang.microfinance.bean.ProductBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.qianchang.microfinance.view.base.BaseRecyclerViewFragment;
import com.qianchang.microfinance.view.credit.data.ConfirmInfoJinbaoActivity;
import com.qianchang.microfinance.view.dialog.alert.DimengSingleButtonAlertDialog;
import com.qianchang.microfinance.view.home.cash.ApplyCashActivity;
import com.qianchang.microfinance.view.home.mortgage.ApplyMortgageActivity;
import com.qianchang.microfinance.view.login.LoginActivity;

import java.lang.reflect.Type;


/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 产品界面
 * 修 改 人: zhoulai
 * 修改时间: 2017/6/26 15:03
 */


public class ProductFragment extends BaseRecyclerViewFragment {


    private static ProductFragment instance;
    public static String PRODUCT_BEAN = "product_bean";
    public static String PRODUCT = "product";
    private FindUserBasicInfoBean basicInfoBean;
    private JsonResult<FindUserBasicInfoBean> response;

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public static ProductFragment getInstance() {
        if (null == instance) {
            synchronized (ProductFragment.class) {
                if (null == instance) {
                    instance = new ProductFragment();
                }
            }
        }
        return instance;
    }

    @Override
    protected View onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return LayoutInflater.from(mContext).inflate(R.layout.fragment_product, container, false);
    }


    @Override
    protected void initView() {
        super.initView();
        setTitle(getString(R.string.product_list));
        setRefreshing(true);
//        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initData();
        }
    }

    @Override
    protected void requestData(HttpParams params) {
        params.put("opSource", 1);
        params.put("maxResults", 10);
        DMOKHttpUtils.getInstance().post(mContext, DMConstant.ApiURL.GET_PRODUCT_LIST, params, mCallBack);
    }

    @Override
    protected BaseRecyclerAdapter<ProductBean> getRecyclerAdapter() {
        return new ProductAdapter(mContext);
    }

    @Override
    protected void onRequestSuccess(String code) {
        super.onRequestSuccess(code);
        getCashStatus();
    }

    private void getCashStatus() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(mContext, DMConstant.ApiURL.FIND_USER_BASIC_INFO, params, new RequestCallBack<FindUserBasicInfoBean>() {

            @Override
            public void onSuccess(JsonResult<FindUserBasicInfoBean> response) {
                ProductFragment.this.response = response;
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    basicInfoBean = response.getObject();
                } else {
                    if (!"300001".equals(response.getCode())) {
                        if (!"300084".equals(response.getCode())) {
                            ToastUtil.getInstant().show(mContext, response.getDescription());
                        }
                    } else {
                        DMApplication.getInstance().setUserLoginInfo(null);
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(mContext, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                ToastUtil.getInstant().show(mContext, msg);
            }

        });
    }

    @Override
    protected Type getType() {
        return new TypeToken<PageBean<ProductBean>>() {
        }.getType();
    }


    @Override
    public void onItemClick(int position, long itemId) {
        super.onItemClick(position, itemId);
        //1.是否登录
        if (DMApplication.getInstance().isLogin()) {
            if (null != response && "300084".equals(response.getCode())) {
                DimengSingleButtonAlertDialog.newInstance("", response.getDescription()).show(getFragmentManager(), getClass().getSimpleName());
                return;
            }
            ProductBean productBean = (ProductBean) mAdapter.getItems().get(position);
            if (null != productBean) {
                String status = productBean.getStatus();
                //2.是否处于停用状态
                if (!"0".equals(status)) {
                    ToastUtil.getInstant().show(mContext, "产品正在维护中，请稍后再试！");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable(ProductFragment.PRODUCT_BEAN, productBean);
                //3.产品类型
                switch (productBean.getProductType()) {
                    case LoanType.JBD://金宝贷
                        if (null != basicInfoBean) {
                            //4.是否已产生金额
                            switch (basicInfoBean.getIsFirstAuth()) {
                                case 1:
                                    //未认证过，去认证
/************************************************************************************************************************************************/
                                    bundle.putString(ConstantAppInfo.PROJECT_TYPE, basicInfoBean.getType());
                                    startActivity(ConfirmInfoJinbaoActivity.class, bundle);
                                    break;
                                case 2:
                                    //正在获取额度
                                    ToastUtil.getInstant().show(mContext, "正在获取额度");
                                    break;
                                case 0: //认证过
                                case 3: //可用额度(已经借款)
                                    bundle.putSerializable(ConstantAppInfo.BASIC_INFO_BEAN, basicInfoBean);
                                    startActivity(ApplyCashActivity.class, bundle);
                                    break;
                            }
                        }
                        break;
                    case LoanType.XSD://学生贷
                        bundle.putString(ConstantAppInfo.PROJECT_TYPE, LoanType.XSD);
                        startActivity(ApplyCashActivity.class, bundle);
                        break;
                    case LoanType.YZD://业主贷
                        bundle.putString(ConstantAppInfo.PROJECT_TYPE, LoanType.YZD);
                        startActivity(ApplyCashActivity.class, bundle);
                        break;
                    case LoanType.DYD://抵押贷
                        bundle.putString(ConstantAppInfo.PROJECT_TYPE, LoanType.DYD);
                        startActivity(ApplyMortgageActivity.class, bundle);
                        break;
                }
            }
        } else {
            startActivity(LoginActivity.class);
        }
    }

}
